<!--- 
@%HEADER%@


Copyright @%CUR_YEAR%@ Seb Duggan

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--->

<cfcomponent extends="BasePlugin">

<!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->	
	<cffunction name="init" access="public" output="false" returntype="any">
		<cfargument name="mainManager" type="any" required="true" />
		<cfargument name="preferences" type="any" required="true" />
		
		<cfset setManager(arguments.mainManager) />
		<cfset setPreferencesManager(arguments.preferences) />
		<cfset setPackage("com/sebduggan/mango/plugins/TwoMangoesPublic") />
				
		<!--- Default preferences --->
		<cfset initSettings(
				podTitle = "Member Login",
				podTextLogin = "Log in below to access the members-only areas of the site.",
				podTextLogout = "You are logged in as {name}.",
				privateHomePage = "/members/"
			) />
		
		<cfreturn this/>
	</cffunction>

<!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->	
	<cffunction name="setup" hint="This is run when a plugin is activated" access="public" output="false" returntype="any">
		<cfreturn "Two Mangoes (Public Site) plugin activated. Would you like to <a href='generic_settings.cfm?event=TwoMangoesPublicSettings&amp;owner=TwoMangoesPublic&amp;selected=TwoMangoesPublicSettings'>configure it now</a>?" />
	</cffunction>
	
	<cffunction name="unsetup" hint="This is run when a plugin is de-activated" access="public" output="false" returntype="any">
		<cfreturn "Two Mangoes (Public Site) plugin de-activated." />
	</cffunction>

<!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->	
	<cffunction name="handleEvent" hint="Asynchronous event handling" access="public" output="false" returntype="any">
		<cfargument name="event" type="any" required="true" />		
		<cfreturn />
	</cffunction>

<!--- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: --->	
	<cffunction name="processEvent" hint="Synchronous event handling" access="public" output="true" returntype="any">
		<cfargument name="event" type="any" required="true" />

			<cfset var LOCAL = StructNew() />
			
			<cfswitch expression="#ARGUMENTS.event.name#">
				
				<cfcase value="getPods">
					<cfset LOCAL.pod = StructNew() />
					<cfset LOCAL.pod.title = getSetting("podTitle") />
					<cfset LOCAL.pod.id = "two-mangoes" />
					<cfsavecontent variable="LOCAL.pod.content"><cfinclude template="display/pod.cfm" /></cfsavecontent>
					<!--- Return the completed pod --->
					<cfset ARGUMENTS.event.addPod(LOCAL.pod) />					
				</cfcase>			
			
			
				<!--- === Admin events === --->
				
				<!--- Pod reference for pod manager --->
				<cfcase value="getPodsList">
					<cfset LOCAL.pod = StructNew() />
					<cfset LOCAL.pod.title = getSetting("podTitle") />
					<cfset LOCAL.pod.id = "two-mangoes" />
					<cfset ARGUMENTS.event.addPod(LOCAL.pod) />
				</cfcase>
			
				<!--- Admin main nav event --->
				<cfcase value="mainNav">
					<cfset LOCAL.link = structnew() />
					<cfset LOCAL.link.owner = "TwoMangoesPublic" />
					<cfset LOCAL.link.address = "#getSetting("privateHomePage")#admin/" />
					<cfset LOCAL.link.title = "Members Admin" />
					<cfset LOCAL.link.icon = "#getAdminAssetPath()#key--arrow.png" />
					
					<cfset ARGUMENTS.event.addLink(LOCAL.link) />
				</cfcase>
			
				<!--- Admin settings nav event --->
				<cfcase value="settingsNav">
					<cfset LOCAL.link = structnew() />
					<cfset LOCAL.link.owner = "TwoMangoesPublic" />
					<cfset LOCAL.link.page = "settings" />
					<cfset LOCAL.link.title = "Two Mangoes" />
					<cfset LOCAL.link.eventName = "TwoMangoesPublicSettings" />
					
					<cfset ARGUMENTS.event.addLink(LOCAL.link) />
				</cfcase>
				
				<!--- Admin settings --->
				<cfcase value="TwoMangoesPublicSettings">
					<cfset LOCAL.data = ARGUMENTS.event.getData() />
					
					<!--- Process if form has been submitted --->			
					<cfif StructKeyExists(LOCAL.data.externaldata,"apply")>
						
						<!--- Update component with new values --->
						<cfset setSettings(
								podTitle = LOCAL.data.externaldata.podTitle,
								podTextLogin = LOCAL.data.externaldata.podTextLogin,
								podTextLogout = LOCAL.data.externaldata.podTextLogout,
								privateHomePage = LOCAL.data.externaldata.privateHomePage
							) />
						
						<!--- Save the new values to the settings file --->
						<cfset persistSettings() />
						
						<!--- Set message to display on screen --->
						<cfset LOCAL.data.message.setstatus("success") />
						<cfset LOCAL.data.message.setType("settings") />
						<cfset LOCAL.data.message.settext("Two Mangoes settings updated") />
					</cfif>
					
					<!--- Include the settings form --->
					<cfsavecontent variable="LOCAL.settingsForm">
						<cfinclude template="admin/settingsForm.cfm" />
					</cfsavecontent>
						
					<!--- change message --->
					<cfset LOCAL.data.message.setTitle("Two Mangoes (Public Site) settings") />
					<cfset LOCAL.data.message.setData(LOCAL.settingsForm) />
				</cfcase>
				
			</cfswitch>
			
		<cfreturn ARGUMENTS.event />
	</cffunction>
	
</cfcomponent>